const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isTabIndex:0,
    userInfo:{}
  },
  getUser: function () {
    let that = this
    app.getUserInfo().then(res => {
      if (res.code == 5009) {
        wx.removeStorageSync('token')
        wx.removeStorageSync('token3')
        return wx.reLaunch({
          url: '/pages/login/login',
        })
      }
      that.setData({
        userInfo: res
      })
    })
  },
  //设备开启
  skipFuwu(){
    let that = this
    if(that.data.userInfo.permission.appletIdList.indexOf('1')==-1){
      that.showDialogt({
        title: '暂无权限',
        icon: 'warning',
        tip: ['暂无【设备开启】权限']
      })
      return
    }
    let hospitalId = that.data.userInfo.hospitalId
    wx.navigateTo({
      url: '/pagesB/deviceon/deviceon?hospitalId='+that.data.userInfo.hospitalId,
    })
  },
  //设备订单
  skipKehu(){
    let that = this
    if(that.data.userInfo.permission.appletIdList.indexOf('3')==-1){
      that.showDialogt({
        title: '暂无权限',
        icon: 'warning',
        tip: ['暂无【设备订单】权限']
      })
      return
    }
    wx.navigateTo({
      url: '/pagesB/orderlist/orderlist',
    })
  },
    //设备绑定
    skipShebei(){
      let that = this
      if(that.data.userInfo.permission.appletIdList.indexOf('2')==-1){
        that.showDialogt({
          title: '暂无权限',
          icon: 'warning',
          tip: ['暂无【设备绑定】权限']
        })
        return
      }
      wx.scanCode({
        success(resCode) {
            if(!resCode.result.match(RegExp(/JJG/))){
              that.showDialog({
                title: '无效二维码',
                icon: 'fail',
                tip: ['请扫描设备背部二维码']
              })
              return
            }
            let deviceNo = ''
            if(!resCode.result.match(RegExp(/TL/))){
              deviceNo = resCode.result.split(';')[2]
            }else{
              deviceNo = resCode.result.split(';')[3]
            }
            wx.request({
              url: app.globalData.serverUrlgdb + '/device/binding',
              method: 'POST',
              header: {
                'token': wx.getStorageSync('token')
              },
              data: {
                deviceNo
              },
              success: (res => {
                let resData = res.data
                if (resData.code == 200) {
                  that.showDialogt({
                    title: '绑定成功',
                    icon: 'success',
                    tip: ['该设备已绑定到本科室']
                  })
                } else {
                  if(resData.message.indexOf(',')){
                    let arr = resData.message.split(',')
                    let arr1 = []
                    arr.map((item, index) => {
                      if (index != 0) {
                        arr1.push(item)
                      }
                    })
                    that.showDialog({
                      title: arr[0],
                      icon: 'fail',
                      tip: [arr1]
                    })
                  }else{
                    that.showDialog({
                      title: resData.message,
                      icon: 'fail',
                      tip: ['']
                    })
                  }
                }
              })
            })
        }
      })
    },
      //数据
  skipShuju(){
    let that = this
    if(that.data.userInfo.permission.appletIdList.indexOf('12')==-1){
      that.showDialogt({
        title: '暂无权限',
        icon: 'warning',
        tip: ['暂无【数据统计】权限']
      })
      return
    }
    wx.navigateTo({
      url: '/pagesB/statisticsb/statisticsb',
    })
  },
  //个人
  skipGeren(){
    let that = this
    wx.navigateTo({
      url: '/pagesB/miens/miens',
    })
  },
  colsFun() {
    let that = this
    that.skipShebei()
  },
  showDialog({
    title,
    tip,
    icon
  } = {}) {
    this.setData({
      title: title,
      tip: tip,
      icon: icon
    })
    this.selectComponent('#dialog').show()
  },
  showDialogt({
    title,
    tip,
    icon
  } = {}) {
    this.setData({
      title: title,
      tip: tip,
      icon: icon
    })
    this.selectComponent('#dialogt').show()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUser()
    console.log(app.globalData.serverUrlgdb);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getUser()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})